package com.jerry.template.module.mainpage.shouye.duanzi.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

import com.jerry.template.R;
import com.jerry.template.base.BaseApplication;
import com.jerry.template.module.mainpage.shouye.duanzi.activity.ActivityJokeDetailActivity;
import com.jerry.template.module.mainpage.shouye.duanzi.activity.TAGDuanziActivity;
import com.jerry.template.module.mainpage.shouye.duanzi.entity.DZDetail;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import de.hdodenhof.circleimageview.CircleImageView;

public class ActivityJokeListContentAdapter<T> extends BaseAdapter {

    private List<DZDetail.DataBean.DataBean1> groupBean = new ArrayList<>();

    private ImageLoader imageLoader;
    private Context context;
    private LayoutInflater layoutInflater;
    private DisplayImageOptions options;
    private int duanziDigg;
    private long groupId;
    private long userId;
    private boolean isFav;

    public ActivityJokeListContentAdapter(Context context, List<DZDetail.DataBean.DataBean1> groupBean) {
        this.context = context;
        this.groupBean = groupBean;
        this.layoutInflater = LayoutInflater.from(context);
        imageLoader = BaseApplication.getApplication().getImageLoader();
        options = BaseApplication.getApplication().getImageOptions();
    }

    @Override
    public int getCount() {
        return groupBean.size();
    }

    @Override
    public DZDetail.DataBean.DataBean1 getItem(int position) {
        return groupBean.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        ViewHolder viewHolder = null;
        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.activity_joke_list_content, null);

            viewHolder = new ViewHolder();

            ButterKnife.bind(viewHolder, convertView);

            convertView.setTag(viewHolder);

        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        DZDetail.DataBean.DataBean1.GroupBean userBean = getItem(position).getGroup();

        if (userBean != null) {
            Log.d("userBean", "================" + userBean);
            Log.d("userBean.getUser()", "================" + userBean.getUser());
            viewHolder.userName.setText(userBean.getUser().getName() + "");

            viewHolder.duanziContent.setText(userBean.getContent() + "");
            Drawable drawable = null;
            if (userBean.isFav()) {
                drawable = context.getResources().getDrawable(R.drawable.ic_digg_pressed);
            } else {
                drawable = context.getResources().getDrawable(R.drawable.ic_digg_normal);
            }
            viewHolder.duanziDigg.setTag(position);
            drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());

            viewHolder.duanziDigg.setCompoundDrawables(drawable, null, null, null);

            viewHolder.duanziBury.setText(userBean.getBury_count() + "");

            viewHolder.duanziDigg.setText(userBean.getDigg_count() + "");

            viewHolder.duanziCommenticon.setText(userBean.getComment_count() + "");

            viewHolder.duanziShare.setText(userBean.getShare_count() + "");
            String userName = userBean.getUser().getName() + "";
            String duanziContent = userBean.getContent() + "";
            duanziDigg = userBean.getDigg_count();
            String duanziBury = userBean.getBury_count() + "";
            String duanziCommenticon = userBean.getComment_count() + "";
            String userHead = userBean.getUser().getAvatar_url() + "";
            groupId = userBean.getGroup_id();
            userId = userBean.getUser().getUser_id();
            isFav = userBean.isFav();
            // 图片显示
            imageLoader.displayImage(
                    userBean.getUser().getAvatar_url(),
                    viewHolder.userHead,
                    options);


            // 设置事件
            convertView.setOnClickListener(
                    new JokeItemClickListener(userName, duanziContent, duanziDigg, duanziBury, duanziCommenticon, userHead));

            viewHolder.duanziDigg.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int position = (int) v.getTag();
                    getItem(position).getGroup().setFav(true);
                    isFav = true;
                    Drawable drawable = context.getResources().getDrawable(R.drawable.ic_digg_pressed);
                    drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
                    ((Button) v).setCompoundDrawables(drawable, null, null, null);
                    duanziDigg += 1;
                    ((Button) v).setText(duanziDigg + "");

                }
            });

            viewHolder.duanziCommenticon.setOnClickListener(
                    new JokeItemClickListener(
                            userName, duanziContent, duanziDigg, duanziBury, duanziCommenticon, userHead));

            viewHolder.duanziTag.setOnClickListener(new tagDuanziClickListener());
        }
        return convertView;
    }

    private class tagDuanziClickListener implements View.OnClickListener {

        @Override
        public void onClick(View v) {
            Intent intent = new Intent();
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.setClass(context, TAGDuanziActivity.class);
            context.startActivity(intent);
        }
    }

    /**
     * 自定义点击事件
     */
    private class JokeItemClickListener implements View.OnClickListener {
        private String userName;
        private String duanziContent;
        private int duanziDigg;
        private String duanziBury;
        private String duanziCommenticon;
        private String userHead;

        public JokeItemClickListener(String userName, String duanziContent,
                                     int duanziDigg, String duanziBury, String duanziCommenticon, String userHead) {
            this.userName = userName;
            this.duanziContent = duanziContent;
            this.duanziDigg = duanziDigg;
            this.duanziBury = duanziBury;
            this.duanziCommenticon = duanziCommenticon;
            this.userHead = userHead;
        }

        @Override
        public void onClick(View v) {
            Intent intent = new Intent();
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.setClass(context, ActivityJokeDetailActivity.class);
            intent.putExtra("userName", userName);
            intent.putExtra("duanziContent", duanziContent);
            intent.putExtra("duanziDigg", duanziDigg);
            intent.putExtra("duanziBury", duanziBury);
            intent.putExtra("duanziCommenticon", duanziCommenticon);
            intent.putExtra("userHead", userHead);
            intent.putExtra("groupId", groupId);
            intent.putExtra("userId", userId);
            intent.putExtra("isFav", isFav);
            context.startActivity(intent);
        }
    }


    protected class ViewHolder {
        @BindView(R.id.user_head)
        CircleImageView userHead;
        @BindView(R.id.user_name)
        TextView userName;
        @BindView(R.id.content)
        TextView duanziContent;
        @BindView(R.id.duanzi_Tag)
        Button duanziTag;
        @BindView(R.id.btn_digg)
        Button duanziDigg;
        @BindView(R.id.btn_bury)
        Button duanziBury;
        @BindView(R.id.btn_commenticon)
        Button duanziCommenticon;
        @BindView(R.id.btn_joke_share)
        Button duanziShare;
    }
}
